package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Random;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AHousing;
import com.ruoyi.system.domain.AUser;
import com.ruoyi.system.mapper.AHousingMapper;
import com.ruoyi.system.mapper.AUserMapper;
import me.chanjar.weixin.common.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ARollMapper;
import com.ruoyi.system.domain.ARoll;
import com.ruoyi.system.service.IARollService;

/**
 * 滚动数据Service业务层处理
 *
 * @author system
 * @date 2025-02-22
 */
@Service
public class ARollServiceImpl implements IARollService {
    @Autowired
    private ARollMapper aRollMapper;

    @Autowired
    private AHousingMapper aHousingMapper;

    @Autowired
    private AUserMapper aUserMapper;
    /**
     * 查询滚动数据
     *
     * @param id 滚动数据主键
     * @return 滚动数据
     */
    @Override
    public ARoll selectARollById(Long id) {
        return aRollMapper.selectARollById(id);
    }

    /**
     * 查询滚动数据列表
     *
     * @param aRoll 滚动数据
     * @return 滚动数据
     */
    @Override
    public List<ARoll> selectARollList(ARoll aRoll) {
        return aRollMapper.selectARollList(aRoll);
    }

    /**
     * 新增滚动数据
     *
     * @param aRoll 滚动数据
     * @return 结果
     */
    @Override
    public int insertARoll(ARoll aRoll) {
        aRoll.setCreateTime(DateUtils.getNowDate());
        return aRollMapper.insertARoll(aRoll);
    }

    /**
     * 修改滚动数据
     *
     * @param aRoll 滚动数据
     * @return 结果
     */
    @Override
    public int updateARoll(ARoll aRoll) {
        aRoll.setUpdateTime(DateUtils.getNowDate());
    return aRollMapper.updateARoll(aRoll);
    }

    /**
     * 批量删除滚动数据
     *
     * @param ids 需要删除的滚动数据主键
     * @return 结果
     */
    @Override
    public int deleteARollByIds(Long[] ids) {
        return aRollMapper.deleteARollByIds(ids);
    }

    /**
     * 删除滚动数据信息
     *
     * @param id 滚动数据主键
     * @return 结果
     */
    @Override
    public int deleteARollById(Long id) {
        return aRollMapper.deleteARollById(id);
    }

    /**
     * 批量逻辑删除滚动数据
     *
     * @param ids 需要删除的滚动数据主键
     * @return 结果
     */
    @Override
    public int logicDeleteARollByIds(Long[] ids) {
        return aRollMapper.logicDeleteARollByIds(ids);
    }
    /**
     * 逻辑删除滚动数据信息
     *
     * @param id 滚动数据主键
     * @return 结果
     */
    @Override
    public int logicDeleteARollById(Long id) {
        return aRollMapper.logicDeleteARollById(id);
    }

    @Override
    public int insertARollNum(ARoll aRoll) {
        String[] cities = {"北京","天津","石家庄","呼和浩特","太原","沈阳","大连","长春","哈尔滨","上海","南京","杭州","宁波","合肥","福州","厦门","南昌",
                "济南","青岛","郑州","武汉","长沙","广州","深圳","南宁","海口","重庆","成都","贵阳","昆明","西安","兰州","西宁","银川",
                "乌鲁木齐","唐山","秦皇岛","包头","丹东","锦州","吉林","牡丹江","无锡","扬州","徐州","温州","金华","蚌埠","安庆","泉州"
                ,"九江","赣州","烟台","济宁","洛阳","平顶山","宜昌","襄阳","岳阳","常德","惠州","湛江","韶关","桂林","北海","三亚","泸州","南充","遵义","大理"};
        String[] surnames = {"赵","钱","孙","李","周","吴","郑","王","冯","陈","褚","卫","蒋","沈","韩","杨","朱","秦","尤","许","何","吕","张","孔","曹","严","华","金","魏","陶",
                "姜"," 戚","谢","邹","喻","柏","水","窦","章","云","苏","潘","葛","奚","范","彭","郎","鲁","韦","昌","马","苗","凤","花","方","俞","任","袁","柳","酆","鲍","史",
                "唐","费","廉","岑","薛","雷","贺","倪","汤","滕","殷","罗","毕","郝","邬","安"};
        String[] call = {"先生", "女士", "经理", "总",};

        String[] times = {"10分钟前", "20分钟前", "30分钟前", "40分钟前","50分钟前","1小时前","2小时前","3小时前","5小时前","8小时前"};

        AHousing ho = new AHousing();
        ho.setIsPay("0");
        List<AHousing> aHousings = aHousingMapper.selectAHousingList(ho);

        for (int z=0; z<Integer.valueOf(aRoll.getNum());z++){
            Random random = new Random();
            int index = random.nextInt(cities.length);
            int surnamesIndex = random.nextInt(surnames.length);
            int callIndex = random.nextInt(call.length);
            int timesIndex = random.nextInt(times.length);
            int hoIndex = random.nextInt(aHousings.size());


            ARoll roll = new ARoll();
            roll.setRollName(cities[index]+"("+surnames[surnamesIndex]+call[callIndex]+")");
            roll.setRollTime(times[timesIndex]);
            roll.setRollDeta(aHousings.get(hoIndex).getAreaDetails());

            // 生成随机手机号前三位
            String middle = "";
            for (int i = 0; i < 8; i++) {
                middle += random.nextInt(10); // 生成8位随机数字
            }
            int suffix = random.nextInt(10); // 最后一位数字
            roll.setRollPhone("13" + middle + suffix);
            if(aRoll.getIsShow() != null && aRoll.getIsShow() == true){
                roll.setIsShow(true);
            }else{
                roll.setIsShow(false);
            }
            roll.setIsType(aRoll.getIsType());
            roll.setCreateTime(DateUtils.getNowDate());
            aRollMapper.insertARoll(roll);
        }

        return 1;
    }

    @Override
    public int insertARollHous(ARoll aRoll) {
        //随机用户头像
        List<AUser> userImg = aUserMapper.selectAUserByHeadImg();
        //随机客户名称
        List<AUser> userName = aUserMapper.selectAUserByNickName();
        //随机时间
        String[] times = {"10分钟前", "20分钟前", "30分钟前", "40分钟前","50分钟前","1小时前","2小时前","3小时前","5小时前","8小时前"};
        //随机店铺情况
        String[] hous = {"查看了该店铺", "收藏了该店铺", "咨询了该店铺"};
        for (int z=0; z<Integer.valueOf(aRoll.getNum());z++){

            Random random = new Random();
            int imgIndex = random.nextInt(userImg.size());
            int nameIndex = random.nextInt(userName.size());
            int timesIndex = random.nextInt(times.length);
            int hoIndex = random.nextInt(hous.length);

            ARoll roll = new ARoll();
            roll.setRollImg(userImg.get(imgIndex).getHeadImg());
            roll.setRollName(userName.get(nameIndex).getNickName());
            roll.setRollTime(times[timesIndex]);
            roll.setRollDeta(hous[hoIndex]);
            if(aRoll.getIsShow() != null && aRoll.getIsShow() == true){
                roll.setIsShow(true);
            }else{
                roll.setIsShow(false);
            }
            roll.setIsType(aRoll.getIsType());
            roll.setCreateTime(DateUtils.getNowDate());
            aRollMapper.insertARoll(roll);
        }

        return 1;
    }

    @Override
    public List<ARoll> selectARollXcxList(ARoll aRoll) {
        return aRollMapper.selectARollXcxList(aRoll);
    }

}
